<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../../react18/react.development.js"></script>
    <script src="../../react18/react-dom.development.js"></script>
    <script src="../../react18/babel.min.js"></script>
  </head>
  <body>
    <div id="app"></div>
    <script type="text/babel">
      /* 
        状态提升：
          一般是为了解决兄弟组件共享数据或传递数据的时候使用
          可以把兄弟组件公有状态数据，交给他们公有的父组件中
          然后通过props的方式，进行父子组件的通信，完成兄弟的通信
      */
      class Child1 extends React.Component {
        render() {
          const { car, changeCar } = this.props;

          return (
            <div>
              <h3>我是哥哥,我开的车是{car}</h3>
              <button onClick={changeCar}>给我们父子三人换辆车</button>
            </div>
          );
        }
      }
      class Child2 extends React.Component {
        render() {
          const { car, changeCar } = this.props;
          return (
            <div>
              <h3>我是弟弟,我开的车是哥哥的车{car}</h3>
              <button onClick={changeCar}>给我们父子三人换辆车</button>
            </div>
          );
        }
      }
      class App extends React.Component {
        state = {
          car: "八手奥拓",
        };
        render() {
          const { car } = this.state;
          return (
            <div>
              <Child1 car={car} changeCar={this.changeCar} />
              <hr />
              <Child2 car={car} changeCar={this.changeCar} />
            </div>
          );
        }

        changeCar = () => {
          this.setState({
            car: "奥迪0000",
          });
        };
      }

      ReactDOM.createRoot(document.getElementById("app")).render(<App />);
    </script>
  </body>
</html>
